#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXNODE = 500100;

typedef struct TDriveway {
	char road;
	int roadId;
	int houseId;
	int distance;
} TDriveway;

int pack(TDriveway x) {
	int v = 0;
	if (x.road=='A') v=50;
	v = v + x.roadId;
	v = v*5000 + x.houseId;
	return v;
}

// TDriveway unpack(int x) {
// 	TDriveway v;
// 	v.houseId = x%5000;
// 	x /= 5000;
// 	v.road = x>=50?'A':'S';
// 	v.roadId = x%50;
// 	return v;
// }


TDriveway src, dst;

bool blocked[MAXNODE];
bool blockIntersect[50][50];
bool visit[MAXNODE];

int qin, qout;
TDriveway queue[MAXNODE];

void markmap(TDriveway x, int a, int b) {
	if (a>b) swap(a,b);
	a = a/2*2;
	b = b/2*2+1;
	for (int i=a; i<=b; ++i) {
		x.houseId = i;
		blocked[pack(x)] = true;
// 		if (i%100==99 && i<b) {
// 			if (x.road == 'S') {
// 				blockIntersect[x.roadId][i/100+1] = true;
// // 				cout << "block at " << x.roadId << " " << i/100+1 << endl;
// 			}else {
// 				blockIntersect[i/100+1][x.roadId] = true;
// // 				cout << "block at " << i/100+1 << " " << x.roadId << endl;
// 			}
// 		}
	}
}

bool illegal(TDriveway x) {
	if (x.roadId<0 || x.roadId>49) return true;
	if (x.houseId<0 || x.houseId>4899) return true;
	if (blocked[pack(x)] || visit[pack(x)]) return true;
	return false;
}

TDriveway turnLeft(TDriveway cur) {
	TDriveway res;

	res.road = 'A'+'S' - cur.road;
	res.roadId = (cur.houseId+1)/100;
	res.houseId = cur.roadId*100+1;

	if (cur.houseId%2==0) {
		if (cur.road=='S') res.houseId -= 3;
	}
	else {
		if (cur.road=='A') res.houseId -= 3;
	}

	return res;
}

TDriveway turnRight(TDriveway cur) {
	TDriveway res;

	res.road = 'A'+'S' - cur.road;
	res.roadId = (cur.houseId+1)/100;
	res.houseId = cur.roadId*100+1;

	if (cur.houseId%2==0) {
		if (cur.road=='A') res.houseId -= 3;
	}
	else {
		if (cur.road=='S') res.houseId -= 3;
	}

	return res;
}

// TDriveway otherDriveway(TDriveway cur) {
// 	TDriveway res;
// 
// 	return cur;
// 	res.road = 'A'+'S' - cur.road;
// 	res.roadId = (cur.houseId+2)/100;
// 
// 	switch (cur.houseId%100) {
// 	case 0:
// 		if (cur.road=='S')
// 			res.houseId = cur.roadId*100+1;
// 		else
// 			res.houseId = (cur.roadId-1)*100+98;
// 		break;
// 	case 98:
// 		if (cur.road=='S')
// 			res.houseId = cur.roadId*100+0;
// 		else
// 			res.houseId = (cur.roadId-1)*100+99;
// 		break;
// 	case 99:
// 		if (cur.road=='S')
// 			res.houseId = (cur.roadId-1)*100+98;
// 		else
// 			res.houseId = cur.roadId*100+1;
// 		break;
// 	case 1:
// 		if (cur.road=='S')
// 			res.houseId = (cur.roadId-1)*100+99;
// 		else
// 			res.houseId = cur.roadId*100+0;
// 		break;
// 	}
// 
// 	return res;
// 	
// }

bool finish(TDriveway x) {
	
// 	if (dst.houseId%100<2 || dst.houseId%100>97) {//has two driveways
// 		if (pack(dst) == pack(x)) return true;
// 		return pack(otherDriveway(dst)) == pack(x);
// 	}
// 	else {
		return (pack(dst) == pack(x));
// 	}
}

// bool safeGoThrough(TDriveway a, TDriveway b) {
// 	if (a.houseId%100==99) {
// 		if (a.road == 'S'){
// 			if (blockIntersect[a.roadId][a.houseId/100+1]) return false;
// 		}
// 		else{
// 			if (blockIntersect[a.houseId/100+1][a.roadId]) return false;
// 		}
// 	}
// 	else if (a.houseId%100==0) {
// 		if (a.road == 'S') {
// 			if (blockIntersect[a.roadId][a.houseId/100]) return false;
// 		}
// 		else {
// 			if (blockIntersect[a.houseId/100][a.roadId]) return false;
// 		}
// 	}
// 	return true;
// }

void search(void) {
	TDriveway newdw[4];
	TDriveway cur, tmpdw;
	int newIdx;

	while (qin>qout) {
		cur = queue[qout++];
		newIdx = 0;
		//produce next node
		if (cur.houseId%100 == 0 || cur.houseId%100 == 99) {
			tmpdw = turnLeft(cur);
			newdw[newIdx++] = tmpdw;
			tmpdw = turnRight(cur);
			newdw[newIdx++] = tmpdw;
		}

		tmpdw = cur;
		if (cur.houseId%2 == 0)
			tmpdw.houseId = cur.houseId-2;
		else
			tmpdw.houseId = cur.houseId+2;
// 		if (safeGoThrough(cur, tmpdw))
			newdw[newIdx++] = tmpdw;

		for (int i=0; i<newIdx; ++i) {
			if (illegal(newdw[i])) {
				swap(newdw[i], newdw[newIdx-1]);
				--newIdx;
				--i;
			}
		}

		if (newIdx == 0) { //U-turn
			tmpdw = cur;
			tmpdw.houseId = tmpdw.houseId/2*2 + 1 - tmpdw.houseId%2;
			if (!illegal(tmpdw))
				newdw[newIdx++] = tmpdw;
		}

		for (int i=0; i<newIdx; ++i) {
			newdw[i].distance = cur.distance+1;
			queue[qin++] = newdw[i];
			visit[pack(newdw[i])] = true;
			if (finish(newdw[i])) {
				return;
			}
		}
	}

}

int main(void)
{

	while (true) {
		char line[10];
		cin >> line;
		if (line[0] == '#') break;
		TDriveway tmpDW;
		tmpDW.road = line[0];
		sscanf(line+1, "%d", &tmpDW.roadId);
		int a, b;
		cin >> a >> b;
		markmap(tmpDW, a, b);
	}
	while (true) {
		char line[10];

		cin >> line;
		if (line[0] == '#') break;
		src.road = line[0];
		sscanf(line+1, "%d", &src.roadId);
		cin >> src.houseId;

		cin >> line;
		dst.road = line[0];
		sscanf(line+1, "%d", &dst.roadId);
		cin >> dst.houseId;

		int minV = 0;

		qout = qin = 0;
		src.distance = 0;
		queue[qin++] = src;
		memset(visit, 0, MAXNODE);
		visit[pack(src)] = true;
		if (!finish(src)) {
			search();
			minV = queue[qin-1].distance-1;
		}
		cout << minV << endl;
	}
	return 0;
}
